package com.unbank.callitnew.action;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.unbank.bhi.dao.BhiDaoImpl;
import com.unbank.callitnew.entity.WebBean;
import com.unbank.util.UrlTools;

public class MyTracker2 {
	private static final WebDriver driver1;
	private static final WebDriver driver2;
	private static Log logger = LogFactory.getLog(MyTracker2.class);
	// HashMap<String, String> columns/
	static {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		// 翻页的Driver
		driver1 = new ChromeDriver();
		driver1.manage().window().maximize();
		// 读取详情页的Driver
		driver2 = new ChromeDriver();
		driver2.manage().window().maximize();
	}

	public void getInfo(WebBean bean) {
		boolean flag = true;
		String url = bean.getUrl();
		// 表的名称
		String tableName = url.replaceAll("(?i)[^a-zA-Z0-9]", "");
		// 首先删除掉表
		BhiDaoImpl.deleteTable(tableName);

		driver1.get(url);
		// 创建表
		boolean isCreate = true;
		Map<String, String> columns = null;

		while (flag) {

			columns = new HashMap<String, String>();
			String frontXpath = bean.getFrontXpath();
			Document frontPageDoc = Jsoup.parse(driver1.getPageSource());
			Elements frontNodes = frontPageDoc.select(frontXpath);
			Element frontNode = null;
			if (frontNodes.size() == 0) {
				logger.info("列表标签Xpath查询出错");
			} else {
				// 得到了列表模块
				frontNode = frontNodes.get(0);
			}
			// 列表模块的标签名称
			String fname = frontNode.tagName();
			logger.info(fname);
			Elements frontAll = frontNode.parent().getElementsByTag(fname);
			// 开始抓取数据
			for (int i = 1; i < frontAll.size(); i++) {

				columns = new HashMap<String, String>();

				Elements tds = frontAll.get(i).getElementsByTag("td");
				if (tds.size() <= 1) {
					continue;
				}
				Elements elements = tds.last().getElementsByTag("a");
				if (elements.size() == 0) {
					elements = tds.get(1).getElementsByTag("a");
				}
				String aurl = elements.first().attr("href");
				String newurl = UrlTools.getFullUrl(url, aurl);
				// 网址链接
				columns.put("detailurl", newurl);
				// 打开详情页面
				driver2.get(newurl);

				Document detailPageDoc = Jsoup.parse(driver2.getPageSource());
				String detailXpath = bean.getDetailXpath().trim();
				Elements detailNodes = detailPageDoc.select(detailXpath);
				Element detailNode = null;
				if (detailNodes.size() == 0) {
					logger.info("没有找到详细页");
				} else {
					detailNode = detailNodes.get(0);
				}
				// 获取详情页的内容
				try {
					columns = getDetail(bean, detailPageDoc, detailNode,
							columns);
				} catch (Exception e) {
					logger.info("获取详情页失败");
					continue;
				}

				if (isCreate) {

					BhiDaoImpl.makeTableSql(tableName, columns);
					BhiDaoImpl.insertSql(tableName, columns);
					isCreate = false;
				} else {
					BhiDaoImpl.insertSql(tableName, columns);
				}
			}
			// 第一页抓取完成下一页
			WebElement userElement = driver1.findElement(By.linkText("下一页"));

			if (userElement != null) {
				logger.info("点击了下一页");
				userElement.click();
			} else {
				break;
			}
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			waitForPageLoaded(driver1);
		}

	}

	// 得到详情页
	private Map<String, String> getDetail(WebBean bean, Document detailPageDoc,
			Element detailNode, Map<String, String> columns) throws Exception {
		Map<String, String> newColumns = new HashMap<String, String>();
		try {
			String dname = detailNode.tagName();
			Elements es = detailNode.parent().getElementsByTag(dname);
			if (dname.equals("tr")) {
				for (int v = 0; v < es.size(); v++) {
					Elements tds1 = es.get(v).getElementsByTag("td");
					int a = 0;
					if (tds1.size() % 2 != 0)
						a = 1;
					for (; a < tds1.size(); a += 2) {
						String text = tds1.get(a + 1).text().trim();
						columns.put(tds1.get(a).text().trim(), text.replace("'", ""));
					}
				}
			} else if (dname.equals("p")) {
				for (int v = 0; v < es.size(); v++) {
					Element p = es.get(v);
					if (p.childNodes().size() == 0)
						continue;
					Elements bs = p.getElementsByTag("b");
					if (bs.size() != 0) {
						String key = bs.text();
						String value = p.text().replaceAll(key, "");
						if(key.contains("案例介绍")){
							value = es.get(v + 1).text();
						}
//						if (value.length() < 2 && (v + 1) <= es.size()) {
//							value = es.get(v + 1).text();
//
////							if (value.trim().isEmpty()) {
////								value = p.nextElementSibling().text();
////								logger.info("找到的值不符合" + value);
////							}
//
//						}
						columns.put(key, value.trim().replace("'", ""));
					} else {

						continue;
					}
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		Map<String, String> attrs = bean.getAttr();
		if (attrs.size() != 0) {
			Set<String> names = attrs.keySet();
			for (String n : names) {
				String xname = n;
				String xvalue = attrs.get(n);
				if (xvalue.equals("custom")) {
					Element e = detailPageDoc.select(xname).first();
					String name = e.text().trim();
					String s = e.parent().text();
					String value = s.substring(s.indexOf(name) + name.length())
							.trim();
					columns.put(name, value.trim().replace("'", ""));
				} else if (xvalue.equals("h4")) {
					Element div = detailPageDoc.select(xname).first();
					Elements h4s = div.select("h4");
					if (h4s.size() != 0) {
						for (Element e : h4s) {
							String name = e.text().trim();
							String value = null;
							if (name.contains("财务摘要")) {
								value = e.nextElementSibling().html();
							} else {
								value = e.nextElementSibling().text().trim();
							}
							columns.put(name, value.trim().replace("'", ""));
						}
					}
				}
			}
		}
		newColumns = columns;
		return newColumns;
	}

	public void waitForPageLoaded(WebDriver driver) {
		ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {
			public Boolean apply(WebDriver driver) {
				return ((JavascriptExecutor) driver).executeScript(
						"return document.readyState").equals("complete");
			}
		};
		Wait<WebDriver> wait = new WebDriverWait(driver, 5000);
		try {
			wait.until(expectation);
		} catch (Throwable error) {
			logger.info(error);
		}
	}

	public MyTracker2() {
	}
}
